package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.BusQingjiaList;
import com.ruoyi.system.domain.BusQingjiaMas;
import com.ruoyi.system.domain.bo.BusQingjiaListBo;
import com.ruoyi.system.domain.bo.BusQingjiaMasBo;
import com.ruoyi.system.domain.dto.BusQingjiaDto;
import com.ruoyi.system.domain.vo.BusQingjiaListVo;
import com.ruoyi.system.domain.vo.BusQingjiaMasVo;
import com.ruoyi.system.mapper.BusQingjiaListMapper;
import com.ruoyi.system.mapper.BusQingjiaMasMapper;
import com.ruoyi.system.service.IBusQingjiaMasService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 请假单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@RequiredArgsConstructor
@Service
public class BusQingjiaMasServiceImpl implements IBusQingjiaMasService {

    private final BusQingjiaMasMapper baseMapper;

    private final BusQingjiaListMapper listMapper;

    /**
     * 查询请假单
     */
    @Override
    public BusQingjiaDto queryById(Long id){
        BusQingjiaMasVo busQingjiaMasVo = baseMapper.selectVoById(id);
        BusQingjiaDto busQingjiaDto = BeanUtil.toBean(busQingjiaMasVo, BusQingjiaDto.class);
        LambdaQueryWrapper<BusQingjiaList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(busQingjiaDto.getBillno()),  BusQingjiaList::getBillno, busQingjiaDto.getBillno());
        List<BusQingjiaList> busQingjiaLists = listMapper.selectBusQingjiaList(lqw);
        busQingjiaLists.forEach(busQingjiaList -> busQingjiaDto.getList().add(BeanUtil.toBean(busQingjiaList,BusQingjiaListVo.class)));
        return busQingjiaDto;
    }


    /**
     * 查询请假单列表
     */
    @Override
    public List<BusQingjiaMasVo> queryList(BusQingjiaMasBo bo) {
        LambdaQueryWrapper<BusQingjiaMas> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusQingjiaMas> buildQueryWrapper(BusQingjiaMasBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusQingjiaMas> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBillno()), BusQingjiaMas::getBillno, bo.getBillno());
        lqw.eq(StringUtils.isNotBlank(bo.getBtype()), BusQingjiaMas::getBtype, bo.getBtype());
        lqw.eq(StringUtils.isNotBlank(bo.getDatetype()), BusQingjiaMas::getDatetype, bo.getDatetype());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptid()), BusQingjiaMas::getDeptid, bo.getDeptid());
        lqw.like(StringUtils.isNotBlank(bo.getDeptname()), BusQingjiaMas::getDeptname, bo.getDeptname());
        lqw.eq(bo.getVoudate() != null, BusQingjiaMas::getVoudate, bo.getVoudate());
        lqw.eq(StringUtils.isNotBlank(bo.getMarker()), BusQingjiaMas::getMarker, bo.getMarker());
        lqw.eq(StringUtils.isNotBlank(bo.getExt01()), BusQingjiaMas::getExt01, bo.getExt01());
        lqw.eq(StringUtils.isNotBlank(bo.getExt02()), BusQingjiaMas::getExt02, bo.getExt02());
        lqw.eq(StringUtils.isNotBlank(bo.getExt03()), BusQingjiaMas::getExt03, bo.getExt03());
        lqw.eq(StringUtils.isNotBlank(bo.getExt04()), BusQingjiaMas::getExt04, bo.getExt04());
        lqw.eq(StringUtils.isNotBlank(bo.getExt05()), BusQingjiaMas::getExt05, bo.getExt05());
        return lqw;
    }

    /**
     * 新增请假单
     */
    @Override
    public Boolean insertByBo(BusQingjiaMasBo bo) {
        BusQingjiaMas add = BeanUtil.toBean(bo, BusQingjiaMas.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertByDto(BusQingjiaDto dto) {
        if(dto.getList().size() == 0){
            throw new ServiceException("明细不能为空");
        }
        BusQingjiaMas add = BeanUtil.toBean(dto, BusQingjiaMas.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            dto.getList().forEach(vo->{
                BusQingjiaList busQingjiaList = BeanUtil.toBean(vo, BusQingjiaList.class);
                busQingjiaList.setBillno(add.getBillno());
                listMapper.insert(busQingjiaList);
            });
           // dto.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改请假单
     */
    @Override
    public Boolean updateByBo(BusQingjiaMasBo bo) {
        BusQingjiaMas update = BeanUtil.toBean(bo, BusQingjiaMas.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusQingjiaMas entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除请假单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
